unit PassForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, PBCheckLB, ExtCtrls, globals, passUnit;

type
  TpassScreen = class(TForm)
    Panel2: TPanel;
    sdLabel1: TLabel;
    Label1: TLabel;
    Label4: TLabel;
    NameEdit: TEdit;
    allKernelsList: TPBCheckListBox;
    Button1: TButton;
    allsubDomainsList: TPBCheckListBox;
    Button7: TButton;
    Panel1: TPanel;
    Button2: TButton;
    Button10: TButton;
    Button11: TButton;
    SaveDialog1: TSaveDialog;
    procedure Button1Click(Sender: TObject);
    procedure allKernelsListClick(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button10Click(Sender: TObject);
    procedure Button11Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
  private
    { Private declarations }
    f : textFile;

  public
    { Public declarations }
  end;

var
  passScreen: TpassScreen;

implementation

uses Contnrs, mainScreen;

{$R *.dfm}

procedure TpassScreen.Button1Click(Sender: TObject);
var
  n : String;
  s, i : integer;
begin
SaveDialog1.FilterIndex := 1;//TEXTURE_MODE - 1000;
if SaveDialog1.Execute then
  begin
    allKernelsList.Clear;
    assignFile(f, SaveDialog1.FileName);
    Reset(f);
    while not EOF(f) do
      begin
        Readln(f, n); // read the name
        allKernelsList.Items.Add(n);
        // skip all other data
        Readln(f, s); // read the size of input textures
        for i:=0 to s-1 do
          Readln(f, n);
        Readln(f, s); // read the size of output textures
        for i:=0 to s-1 do
          Readln(f, n);
        Readln(f, n); // skip the line seperator
      end;
    closeFile(f);
  end;
end;



procedure TpassScreen.allKernelsListClick(Sender: TObject);
var
  tmp : String;
begin
if countChecked(allKernelsList, tmp) > 1 then
  begin
    showMessage('Only One Kernel selection is allowed');
    allKernelsList.State[allKernelsList.ItemIndex] := cbUnchecked;
  end;
end;

procedure TpassScreen.Button2Click(Sender: TObject);
var
 p : PassClass;
 lastSelected : String;
 i : Integer;
begin
if countChecked(allKernelsList, lastSelected) <> 0 then
  begin
    if NameEdit.Text <> '' then
      begin
        p := PassClass.Create(NameEdit.Text, lastSelected);
        for i:=0 to allsubDomainsList.Count-1 do
          if allsubDomainsList.State[i] = cbchecked then
            p.add_SubDomain(allsubDomainsList.items.Strings[i]);
        passList.Add(p);
        // clear the GUI
        NameEdit.Text := '';
        ActiveControl := NameEdit;
        for i:=0 to allKernelsList.Count-1 do
          allKernelsList.State[i] := cbUnChecked;
        for i:=0 to allsubDomainsList.Count-1 do
          allsubDomainsList.State[i] := cbUnChecked;

        // add pass name in the global view screen
        globalView.passesList.State[globalView.passesList.Items.Add(p.get_Name())] := cbchecked;
      end
    else
      showMessage('You Have to Name this Pass...');
  end
else
  showMessage('One Kernel has to be selected first...');
end;

procedure TpassScreen.Button10Click(Sender: TObject);
var
  i, j, s : Integer;
  p : PassClass;
begin
SaveDialog1.FilterIndex := 3;
if SaveDialog1.Execute then
  begin
    assignFile(f, SaveDialog1.FileName);
    if FileExists(SaveDialog1.FileName) then
       append(f)
    else Rewrite(f);
    for i:=0 to passList.Count-1 do
      begin
        p := PassClass(passList.Items[i]);
        writeLn(f,p.get_Name());
        writeLn(f,p.get_kernelName);
        s := p.get_SubDomainCount();
        writeLn(f, s);//the size of the subdomain
        for j:=0 to s-1 do
          writeLn(f, p.get_SubDomain(j));
        writeLn(f, '*****************************'); // line seperator
      end;
    passList.Clear;
    closeFile(f);
  end;
end;

procedure TpassScreen.Button11Click(Sender: TObject);
begin
close();
end;

procedure TpassScreen.Button7Click(Sender: TObject);
var
  n : String;
begin
SaveDialog1.FilterIndex := 2;
if SaveDialog1.Execute then
  begin
    allsubDomainsList.Clear;
    assignFile(f, SaveDialog1.FileName);
    Reset(f);
    while not EOF(f) do
      begin
        Readln(f, n); // read the name
        allsubDomainsList.Items.Add(n);
        // skip all other data
        Readln(f, n); Readln(f, n);Readln(f, n); Readln(f, n);
        Readln(f, n);
      end;
    closeFile(f);
  end;
end;

end.
